package com.movitech.mobile.cz.base.sys.controller;


import com.movitech.mobile.cz.base.sys.bizvo.ArticleResponseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.sys.bizvo.ArticleListRequestVO;
import com.movitech.mobile.cz.base.sys.bizvo.ArticleRequestVO;
import com.movitech.mobile.cz.base.sys.entity.SArticle;
import com.movitech.mobile.cz.base.sys.service.SArticleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * <p>
 * SArticle 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@RestController
@RequestMapping("/v1/sArticle/public")
@Api(value = "文章基础操作接口", description = "文章基础操作接口")
public class SArticleController {
    @Autowired
    @Qualifier("sArticleService")
    private SArticleService sArticleService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有文章接口",
            notes = "获取所有文章接口",
            response = SArticle.class
    )
    public RestReturnResult list() {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SArticle> page = sArticleService.selectPage(new Page<SArticle>());
        restReturnResult.setData(page);
        return restReturnResult;
    }

//    @PostMapping("/searchList/{pageNumber}")
//    @ApiOperation(
//            value = "文章列表查询接口",
//            notes = "文章列表查询接口",
//            response = SArticle.class
//    )
//    public RestReturnResult searchList(@RequestBody SArticle sArticle,
//                @ApiParam(value = "分页页数")@PathVariable Integer pageNumber) {
//        RestReturnResult restReturnResult = RestReturnResult.success();
//        Page<SArticle> page = new Page<>();
//        page.setCurrent(pageNumber);
//        EntityWrapper wrapper = new EntityWrapper<>();
//        wrapper.setEntity(sArticle);
//        page = sArticleService.selectPage(page,wrapper);
//        restReturnResult.setData(page);
//        return restReturnResult;
//    }

    @GetMapping("/getSArticle/{id}")
    @ApiOperation(
            value = "文章对象ID获取接口",
            notes = "文章对对象ID获取接口",
            response = SArticle.class
    )
    public RestReturnResult addSArticle(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        SArticle sArticle = sArticleService.selectById(id);
        restReturnResult.setData(sArticle);
        return restReturnResult;
    }

	@GetMapping("/getArticleResponseVOById/{id}")
	@ApiOperation(
			value = "帮助中心详情接口",
			notes = "帮助中心详情接口",
			response = SArticle.class
	)
	public RestReturnResult getArticleResponseVOById(@PathVariable String id) {
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL,id, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		try{
			SArticle sArticle = sArticleService.selectById(id);
			if(sArticle==null){
				hResult = new RestReturnResult<Object>(CommonConstants.FAIL,"详情不存在");
			}else{
				ArticleResponseVO articleResponseVO=new ArticleResponseVO();
				BeanUtils.copyProperties(sArticle,articleResponseVO);
				hResult=RestReturnResult.success();
				hResult.setData(articleResponseVO);
			}
		} catch (RuntimeException e) {
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL,id, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		}
		return hResult;
	}

//    @PostMapping("/add")
//    @ApiOperation(
//            value = "文章添加接口",
//            notes = "文章添加接口",
//            response = SArticle.class
//    )
//    public RestReturnResult add(@RequestBody SArticle sArticle) {
//        RestReturnResult restReturnResult = RestReturnResult.success();
//        sArticleService.insert(sArticle);
//        restReturnResult.setData(sArticle);
//        return restReturnResult;
//    }
//
//    @PutMapping("/update")
//    @ApiOperation(
//            value = "文章更新接口",
//            notes = "文章更新接口",
//            response = SArticle.class
//    )
//    public RestReturnResult update(@RequestBody SArticle sArticle) {
//        RestReturnResult restReturnResult = RestReturnResult.success();
//        sArticleService.updateById(sArticle);
//        restReturnResult.setData(sArticle);
//        return restReturnResult;
//    }
//
//    @DeleteMapping("/delete/{id}")
//    @ApiOperation(
//            value = "文章通过ID删除接口",
//            notes = "文章通过ID删除接口",
//            response = SArticle.class
//    )
//    public RestReturnResult update(@PathVariable String id) {
//        RestReturnResult restReturnResult = RestReturnResult.success();
//        sArticleService.deleteById(id);
//        return restReturnResult;
//    }
    
    
    /**
	 * 文章列表获取接口 (文章类型（1：帮助中心）)
	 * @param articleListRequestVO
	 * @return
	 */
    @ApiOperation(
            value = "文章列表获取接口",
            notes = "文章列表获取接口",
            response = SArticle.class
    )
	@RequestMapping(value = "/getArticleListByType", method = RequestMethod.POST)
	@ResponseBody
	public RestReturnResult<Object> getArticleListByType(@RequestBody ArticleListRequestVO articleListRequestVO){
		RestReturnResult restReturnResult = RestReturnResult.success();
	    Page<SArticle> page = new Page<>();
	    page.setCurrent(articleListRequestVO.getPageNumber());
        EntityWrapper wrapper = new EntityWrapper<>();
        SArticle sArticle = new SArticle();
        sArticle.setArticleType(articleListRequestVO.getArticleType());
        wrapper.setEntity(sArticle);
        page = sArticleService.selectPage(page,wrapper);
        restReturnResult.setData(page);
		return restReturnResult;
		
	}


	/**
	 *帮助中心列表接口
	 * @param articleRequestVO
	 * @return
	 */
	@ApiOperation(
			value = "帮助中心列表接口",
			notes = "帮助中心列表接口",
			response = SArticle.class
	)
	@RequestMapping(value = "/getHelpCenter", method = RequestMethod.POST)
	@ResponseBody
	public RestReturnResult<Object> getHelpCenter(@RequestBody ArticleRequestVO articleRequestVO){
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL,articleRequestVO, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		try{
			hResult=sArticleService.getHelpCenter(articleRequestVO);
		} catch (Exception e) {
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL,articleRequestVO, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		}
		return hResult;
	}


    /**
	 * 文章新增 接口
	 * @param articleRequestVO
	 * @return
	 */
    @ApiOperation(
            value = "文章新增接口",
            notes = "文章新增接口",
            response = SArticle.class
    )
	@RequestMapping(value = "/saveOrUpdateArticle", method = RequestMethod.POST)
	@ResponseBody
	public RestReturnResult<Object> saveOrUpdateArticle(@RequestBody ArticleRequestVO articleRequestVO){
    	RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		
		try{
			hResult = sArticleService.saveOrUpdateArticle(articleRequestVO);
		}catch(Exception e){
			e.printStackTrace();
			hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
		}
		
		return hResult;
	}
	
	/**
	 * 文章删除接口
	 * @param articleRequestVO
	 * @return
	 */
    @ApiOperation(
            value = "文章删除接口",
            notes = "文章删除接口",
            response = SArticle.class
    )
	@RequestMapping(value = "/deleteArticle", method = RequestMethod.POST)
	@ResponseBody
	public RestReturnResult<Object> deleteArticle(@RequestBody ArticleRequestVO articleRequestVO){
    	RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		
		try{
			hResult = sArticleService.updateArticleForDel(articleRequestVO);
		}catch(Exception e){
			e.printStackTrace();
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
		}
		
		return hResult;
	}
    
}

